package com.morningstar.keyratio;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import com.morningstar.keyratio.KeyRatioEntity.KeyIndex;
import com.morningstar.keyratio.KeyRatioEntity.Section;
import com.morningstar.keyratio.KeyRatioEntity.SubSection;

@Deprecated
public class KeyRatioEntityJsonTranslator{

	public static class KeyRatioEntityTranslator implements JsonSerializer<KeyRatioEntity>{
		
		public JsonElement serialize(KeyRatioEntity src, Type typeOfSrc, JsonSerializationContext context) {
	        JsonObject jobj = new JsonObject();
	        jobj.addProperty("title", src.getTitle());
	        
	        //GsonBuilder builder = new GsonBuilder();
            //builder.registerTypeAdapter(KeyRatioEntity.Section.class, new KeyRatioEntityJsonTranslator.SectionTranslator());
	        //jobj.add("sections",builder.create().toJsonTree(src.getSections(), new TypeToken<List<Section>>() {}.getType()));
	        
	        final JsonElement jsonSections = context.serialize(src.getSections());
	        jobj.add("sections", jsonSections);
	        
	        return jobj;
	   }
	}
	
	public static class SectionTranslator implements JsonSerializer<KeyRatioEntity.Section>{
		public JsonElement serialize(Section src, Type typeOfSrc, JsonSerializationContext context) {
            JsonObject jobj = new JsonObject();
            jobj.addProperty("title---", src.getTitle());
            
            //GsonBuilder builder = new GsonBuilder();
            //builder.registerTypeAdapter(KeyRatioEntity.SubSection.class, new KeyRatioEntityJsonTranslator.SubSectionTranslator());
            //jobj.add("subSections", builder.create().toJsonTree(src.getSubSections(), new TypeToken<List<SubSection>>() {}.getType()));
            
            final JsonElement jsonSubSections = context.serialize(src.getSubSections());
            
	        jobj.add("subSections", jsonSubSections);
            
            return jobj;
       }
	}
	
	public static class SubSectionTranslator implements JsonSerializer<KeyRatioEntity.SubSection>{
		public JsonElement serialize(SubSection src, Type typeOfSrc, JsonSerializationContext context) {
            JsonObject jobj = new JsonObject();
            jobj.addProperty("title?????", src.getTitle());
            
            /*GsonBuilder builder = new GsonBuilder();
            builder.registerTypeAdapter(KeyRatioEntity.KeyIndex.class, new KeyRatioEntityJsonTranslator.KeyIndexTranslator());
            jobj.add("indices", builder.create().toJsonTree(src.getIndices(), new TypeToken<List<KeyIndex>>() {}.getType()));
            jobj.add("years", new Gson().toJsonTree(src.getYears(), new TypeToken<List<String>>() {}.getType()));*/
            return jobj;
       }
	}
	
	public static class KeyIndexTranslator implements JsonSerializer<KeyRatioEntity.KeyIndex>{
		public JsonElement serialize(KeyIndex src, Type typeOfSrc, JsonSerializationContext context) {
            JsonObject jobj = new JsonObject();
            jobj.addProperty("title", src.getTitle());
            jobj.add("data", new Gson().toJsonTree(src.getData(), new TypeToken<List<Float>>() {}.getType()));
            return jobj;
       }
	}

	
	
}
